<?php
define('IN_TG','member_modify');
define('IN_JS','member_modify');
define('SCRIPT','member_modify');
session_start();
require('includes/common.inc.php');
//加载头部文件
require('header.php');

$sql="select tg_username,tg_sex,tg_face,tg_email,tg_url,tg_qq,tg_switch,tg_autograph from tg_user where tg_username ='{$_SESSION['username']}' limit 1";
$res = _query($sql);
$row=_fetch_array_list($res);
$face_list=array();
$dir=scandir("./face");
foreach ($dir as $file_name)
{
	//过滤$file_name为.和..的情况
	if($file_name!='.' && $file_name!='..')
	{
		//判断当前$path.'/'.$file_name是否是目录
		if(is_file("./face/".$file_name))
		{
			//如果是文件，则进行递归，返回数组结果，设置数组的键欸目录名
			$face_list[]="face/".$file_name;
		}
	}
}
if(!empty($_POST))
{
	if($_GET['action']=='modify')
	{
		include ROOT_PATH.'includes/check.func.php';
		_check_code($_POST['code'],$_SESSION['code']);
		$sex = _check_sex($_POST['sex']);
		$face = _check_face($_POST['face']);
		$email =  _check_email($_POST['email'],5,40);
		$qq = _check_qq($_POST['qq']);
		$url = _check_url($_POST['url'],40);
		$switch=$_POST['switch'];
		$autograph=$switch==1?_check_autograph($_POST['autograph'],20):'';
		$password =strlen($_POST['password'])!=0?"tg_password='".md5($_POST['password'])."',":'';
		$sql = "update tg_user set tg_sex='{$sex}',{$password} tg_face='{$face}',tg_email='{$email}',tg_qq='{$qq}',tg_url='{$url}',tg_switch={$switch},tg_autograph='{$autograph}' where tg_username='{$_SESSION['username']}'";
		$res = _query($sql);
		if($res){
			header('Location:member.php');
		}
		else
		{
			_alert_back("修改失败");
		}
	}
}
?>
<div id="member">
	<?php
		require ("includes/member.inc.php");
	?>
	<div id="member_main">
		<h2>修改资料</h2>
		<form method="post" action="?action=modify">
		<dl>
			<dd>用 户 名：<?php echo $row['tg_username'];?></dd>
			<dd>密　　码：<input type="password" class="text" name="password" /> (留空则不修改)</dd>
			<dd>性　　别：<input type="radio" name="sex" value="男" <?php echo $row['tg_sex']=='男'?'checked':'';?>/> 男 <input type="radio" name="sex" value="女"  <?php echo $row['tg_sex']=='女'?'checked':'';?>/> 女</dd>
			<dd>头　　像：<select name="face" id="face">
					<?php foreach ($face_list as $value):?>
					<option value="<?php echo $value;?>" <?php echo $row['tg_face']==$value?'selected':'';?>><?php echo $value;?></option>
					<?php endforeach;?>
				</select>
				<img src="<?php echo $row['tg_face'];?>" alt="头像选择" id="faceimg" align="center"/>
			</dd>
			<dd>电子邮件：<input type="text" class="text" name="email" value="<?php echo $row['tg_email'];?>" /></dd>
			<dd>主　　页：<input type="text" class="text" name="url" value="<?php echo $row['tg_url'];?>" /></dd>
			<dd>Q 　 　Q：<input type="text" class="text" name="qq" value="<?php echo $row['tg_qq'];?>" /></dd>
			<dd>个性签名：<input type="radio" name="switch" value="1" <?php echo $row['tg_switch']==1?'checked':'';?>/> 启用 <input type="radio" name="switch" value="0" <?php echo $row['tg_switch']==0?'checked':'';?>/> 禁用 (可以使用UBB代码)
				<p><textarea name="autograph"><?php echo $row['tg_autograph'];?></textarea></p>
			</dd>
			<dd>验 证 码：<input type="text" name="code" class="text yzm"  /> <img src="code.php" id="code" onclick="javascript:this.src='code.php?tm='+Math.random();" /> <input type="submit" class="submit" value="修改资料" /></dd>
		</dl>
		</form>
	</div>
</div>

<?php
require('includes/footer.inc.php');
?>
